package com.zjgsu.nftplatform.util;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author ahy231
 * @date 2021/8/14 13:28
 * @description
 */
@Component
public class RoleAssert {
    public static void hasAllRole(String... roles) {
        Assert.isTrue(SecurityUtils.getSubject().hasAllRoles(Arrays.stream(roles).collect(Collectors.toSet())), "用户没有权限: " + Arrays.toString(roles));
    }

    public static void hasOneRole(String... roles) {
        boolean[] checks = SecurityUtils.getSubject().hasRoles(Arrays.asList(roles));
        for (boolean check : checks) {
            if (check) return;
        }
        throw new RuntimeException("用户没有权限：" + Arrays.toString(roles));
    }
}
